package com.yuqing.sentimentapp.home.acitivity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linkface.common.adaper.CommonViewHolder;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.home.bean.SpecialTopicDetailDataBean;
import com.yuqing.sentimentapp.home.presenter.SpecialTopicDetailPresenter;
import com.yuqing.sentimentapp.home.presenter.impl.SpecialTopicDetailPresenterImpl;
import com.yuqing.sentimentapp.home.view.SpecialTopicDetailView;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.widget.xrecyclerview.XRecyclerView;
import com.yuqing.sentimentapp.yqlist.activity.YuQingDetailActivity;
import com.yuqing.sentimentapp.yqlist.adapter.YqListAdapter;
import com.yuqing.sentimentapp.yqlist.bean.YqListResultBean;
import com.yuqing.sentimentapp.yqlist.bean.YuQingDataBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by shenhe on 2019/4/30.
 */
public class SpecialTopicDetailActivity extends BaseActivity implements SpecialTopicDetailView<SpecialTopicDetailDataBean> {
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.title_rooter)
    RelativeLayout mTitleRooter;
    @BindView(R.id.recyclerView)
    XRecyclerView mRecyclerView;
    private String title;
    private List<YuQingDataBean> dataList = new ArrayList<>();
    private YqListAdapter mAdapter;
    private SpecialTopicDetailPresenter mPresenter;
    private String topicId;

    public static void start(Context context, String topicId, String title) {
        Intent intent = new Intent(context, SpecialTopicDetailActivity.class);
        intent.putExtra("topicId", topicId);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_special_topic_detail;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SpecialTopicDetailPresenterImpl(this);
    }

    @Override
    public void initView() {
        initIntentData();
        initTitle();
        initRecyclerView();
        loadData();
    }

    private void initIntentData() {
        Intent intent = getIntent();
        if (intent == null) return;
        title = intent.getStringExtra("title");
        topicId = intent.getStringExtra("topicId");
    }

    private void initTitle() {
        mTvTitle.setText(title);
        mTvTitle.setMaxEms(10);
        mTvTitle.setSingleLine();
        mTvTitle.setEllipsize(TextUtils.TruncateAt.END);
    }

    private void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setLoadingMoreEnabled(false);
        mRecyclerView.setPullRefreshEnabled(false);

        mAdapter = new YqListAdapter(this, dataList, new CommonViewHolder.onItemCommonClickListener() {
            @Override
            public void onItemClickListener(int position) {
                YuQingDetailActivity.start(SpecialTopicDetailActivity.this, dataList.get(position - 1).getBdArticleId(), position - 1);
            }

            @Override
            public void onItemLongClickListener(int position) {

            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    private void loadData() {
        mPresenter.loadSpecialTopicDetailRequest(topicId);
    }

    @OnClick(R.id.iv_back)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void returnSpecialTopicDetaiResult(SpecialTopicDetailDataBean result) {
        if (result == null) return;
        SpecialTopicDetailDataBean.DataBean data = result.getData();
        if (data == null) return;
        dataList.clear();
        dataList.addAll(data.getList());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onSuccess(SpecialTopicDetailDataBean data) {

    }

    @Override
    public void onFailure(String message) {
        ToastUtil.showShort(message);
    }
}
